<template>
	<view class="main">
		<TopBar :indexflag="true" :combakflag="true" :lang="false" :bottom="true" :title="$t('MerchandiseManagement')"
			barstyle="#45bc1b" Color="#fff"></TopBar>
		<view class="shop-list" :dir="i18nLocale=='ug'?'rtl':''">
			<view class="shop-item" v-for="(item,index) in productList"  @click="getProductone(item.id)" >
				<view class="left">
					<image :src="getImgSrc(item.img)" mode=""></image>
					<view class="shop-content">
						<view class="title">{{i18nLocale=='ug'?item.ug_name:item.cn_name}}</view>
						<view class="">￥{{item.price}}</view>
						<view class="sub-title">
							<text class="">{{$t('Sales')}}: {{item.sold}} </text>
							<text style="width:30rpx"></text>
							<text class=""> {{$t('Inventory')}}: {{item.stock}}</text>
						</view>

					</view>
				</view>
				<view class="right">
					<view class="button edit">
						<u-icon name="edit-pen" color="#f9ae3d" size="22"></u-icon>
						<text>{{$t('EditShop')}}</text>
					</view>
					
					<view class="button delet" @click="deletproduct(item.id)">
						<u-icon name="tags" :color="item.status?'#45BC1B':'#cc0000'" size="22"></u-icon>
						<text class="" :style="{color:item.status?'#45BC1B':'#cc0000'}">{{item.status? $t('Shelves'):$t('TakenShelves')}}</text>
					</view>
				</view>
			</view>
		</view>
		<view class="footer-button" @click="addproduct">
			<u-icon name="plus-circle" color="#45BC1B" size="22"></u-icon>
			<text>{{$t('AddShop')}}</text>
		</view>
		<u-popup :show="popshow" :round="10" mode="center" @close="close" @open="open">
			<view :class="i18nLocale=='ug'?'ug-from pop-box' :'pop-box'" :dir="i18nLocale=='ug'?'rtl':''">
				<view class="pop-title">{{shopname}} {{$t('Edit')}}</view>
					<view class="input-item">
						<view class="title">
							{{$t('ShopPrice')}}
						</view>
						<u--input v-model="price" :placeholder="$t('ShopPricePlaceholder')" type="number" fontSize="28rpx"
							:adjustPosition='false'></u--input>
					</view>
					<view class="input-item">
						<view class="title">
							{{$t('ShopType')}}
						</view>
						<view class="" :style="{color:status?'#45BC1B':'#cc0000'}">{{status? $t('Shelves'):$t('TakenShelves')}}</view>
						<u-switch dir="ltr" v-model="status" @change="change" activeColor="#45BC1B"  ></u-switch>
					</view>
					<view class="button-grop">
						<view class="button" style="color:#45BC1B" @click="editshop" >{{$t('Confirm')}}</view>
						<view class="button" @click="popshow=false">{{$t('TimeCancel')}}</view>
					</view>
			</view>
		</u-popup>
		<u-toast ref="uToast" :message="notmessage"></u-toast>
	</view>
</template>

<script>
	import TopBar from "@/components/TopBar.vue"
	import {
		getproducts,
		getproductone,
		putproducts 
	} from '@/api/all.js';

	export default {
		components: {
			TopBar,

		},
		data() {
			return {
				productList: [],
				notmessage: '请仔细填写!',
				popshow: false,
				status:false,
				price:'',
				shopname:'',
				shopid:''
			};
		},
		onLoad() {
			this.getProducts()
		},
		computed: {
			i18nLocale() {
				return this.$i18n.locale;
			},
		},
		methods: {
			getProducts() {
				let that = this;
				getproducts({}).then((res) => {
					if (res.code == 200) {
						let data = res.body.data;
						that.productList = data;
						console.log(res, '++++')
					}
				})
			},
			//修改
			getProductone(id) {
				let that=this;
				that.shopid=id;
				getproductone({id:id}).then((res)=>{
					console.log(res,'++++')
					if(res.code==200){
						let data=res.body.data;
					that.status=data.status;
					that.price=data.price;
					if(that.$i18n.locale=='ug'){
						that.shopname=data.ug_name
					}else{
						that.shopname=data.cn_name
					}
					
					}
				})
				this.popshow = true
			},
			change(e){
				this.status=e;
			},
			//提交修改
			editshop(){
				let that =this;
				putproducts({
					id:that.shopid,
					status:that.status,
					price:that.price
				}).then((res)=>{
					if(res.code==200){
						that.getProducts();
					}
				})
				this.popshow = false
			},
			open() {
				// console.log('open');
			},
			close() {
				this.popshow = false
				// console.log('close');
			},
			//删除
			deletproduct(id) {

			},
			//添加商品
			addproduct() {
				this.$refs.uToast.show({
					top: 0,
					message: this.$t('PleaseCustomerService'),
					color: '#fff',
					bgColor: '#f0ba53'
				});
				return;
				// uni.navigateTo({
				// 	url:'/pages/me/addshop'
				// })
			}
		}
	}
</script>

<style lang="scss">
	.main {
		width: 100%;
		height: 100vh;
		position: relative;
	}

	.shop-list {
		width: 100%;
		height: 100%;

		.shop-item {
			width: 100%;
			display: flex;
			align-items: center;
			justify-content: space-between;
			box-sizing: border-box;
			background-color: $uni-bg-color-box;
			border-radius: $uni-border-radius-base;
			box-shadow: $uni-bg-shadow;
			margin-top: 30rpx;
			padding: $uni-spacing-padding-sm;

			.left {

				display: flex;
				align-items: center;

				image {
					width: 200rpx;
					height: 200rpx;
					border-radius: $uni-border-radius-lg;
				}

				.shop-content {
					height: 200rpx;
					box-sizing: border-box;
					padding: 20rpx;

					.title {
						color: $uni-color-title;
						font-size: $uni-font-size-lg ;
					}

					.sub-title {
						color: $uni-text-color-grey;
						font-size: $uni-font-size-sm0;
						display: flex;
						justify-content: space-between;

					}

					display: flex;
					flex-direction: column;
					justify-content: space-around;
				}
			}

			.right {
				display: flex;
				align-items: center;
				color: $uni-text-color-grey;
				font-size: $uni-font-size-sm1;

				.button {
					padding: 5rpx 10rpx;
					border-radius: $uni-border-radius-lg;
					margin: 0rpx 10rpx;
					display: flex;
					flex-direction: column;
					align-items: center;
					justify-content: center;

					text {
						padding: 10rpx 0rpx;
					}
				}

			}
		}

	}

	.footer-button {
		width: 100%;
		height: 100rpx;
		background-color: #fff;
		display: flex;
		align-items: center;
		justify-content: center;
		position: fixed;
		bottom: 0;
		left: 0;

		text {
			padding: 0rpx 20rpx;
		}
	}
	.pop-box{
		width:600rpx;
		height:500rpx;
		.pop-title{
			display: flex;
			align-items: center;
			justify-content: center;
			padding: 20rpx 0rpx;
		}
		.input-item {
			width: 100%;
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding: $uni-spacing-padding-sm  40rpx;
			box-sizing: border-box;
			margin-top: 20rpx;
		
			input {
				font-size: $uni-font-size-base;
				border: 1rpx solid #999;
			}
		
			.title {
				color: $uni-color-title;
				font-size: $uni-font-size-base;
				padding:0rpx 10rpx;
				// padding: 0rpx 30rpx;'
			}
		}
		.button-grop{
			display: flex;
			justify-content: space-around;
			margin-top:50rpx;
			.button{
				border: 1rpx solid #999;
				padding: 10rpx 30rpx;
				border-radius: 20rpx;
			}
		}
	}
	.ug-from {
		direction: rtl;
	
		/deep/ .uni-input-input {
			direction: rtl !important;
			text-align: right;
		}
	
		/deep/ .uni-input-placeholder {
			text-align: right;
		}
	}
</style>